using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Presentation.Contracts;
using Presentation;

namespace ProjektZespolowy.Forum
	{
	public partial class SearchForums : System.Web.UI.Page, ISearchForumsView
		{

		private SearchForumsPresenter presenter;
		private DataTable searchedForumsTable;

		protected override void OnInit(EventArgs e)
			{
			base.OnInit(e);
			presenter = new SearchForumsPresenter(this);
			}

		protected void Page_Load(object sender, EventArgs e)
			{
			if (!IsPostBack)
				{
				presenter.Initialize();
				if (Request.QueryString["SearchText"]!= null)
					{
					this.SearchedString = Request.QueryString["SearchText"].ToString();
					if (this.SearchedString.Length > 0)
						{
						presenter.SearchForums();
						}
					}
				if (this.LoggedUserID < 0)
					{
					GridView1.Columns[0].Visible = false;
					}
				}
			}

		public int LoggedUserID
			{
			get
				{
				if (Session[Globals.ContentNamesGlobals.SessionWithUserId] != null)
					return Int32.Parse(Session[Globals.ContentNamesGlobals.SessionWithUserId].ToString());
				else
					return -1;
				}
			}

		#region ISearchForumsView Members

		public void ShowSearchedForums(DataTable dt)
			{
			ViewState["tabelka"] = dt;
			GridView1.DataSource = ViewState["tabelka"];
			GridView1.DataBind();
			}

		public string SearchedString
			{
			get
				{
				return TextBox1.Text; 
				}
			set
				{
				TextBox1.Text = value;
				}
			}

		#endregion

		protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
			{
			GridView1.DataSource = ViewState["tabelka"];
			GridView1.PageIndex = e.NewPageIndex;
			GridView1.DataBind();
			}

		protected void Button1_Click(object sender, EventArgs e)
			{
			if (TextBox1.Text.Length > 0)
				{
				this.SearchedString = TextBox1.Text;
				presenter.SearchForums();
				}
			}

		protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
			{
			HyperLink tmp = (HyperLink)GridView1.Rows[e.RowIndex].Cells[1].Controls[1];
			string a = tmp.NavigateUrl;
			string[] tab = a.Split('=');
			int forum_id = Int32.Parse(tab[1]);
			presenter.SignMeToForum(forum_id);
			Response.Redirect("~/Forum/Default.aspx");
			}
		}
	}
